﻿
using ShunFengApi.Enum;
using ShunFengApi.Model;
using ShunFengApi.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShunFengApi
{
    public class ShunFengHelper
    {
        #region 创建快递单 + bool CreateOrder(OrderInfoModel orderInfo, string transMessageId)
        /// <summary>
        /// 创建快递单
        /// </summary>
        /// <param name="orderInfo">快递单信息</param>
        /// <param name="transMessageId">新的交易流水号（保证唯一即可）</param>
        /// <returns></returns>
        public bool CreateOrder(OrderInfoModel orderInfo, string transMessageId)
        {
            if (orderInfo == null || orderInfo.cargoInfo == null || orderInfo.consigneeInfo == null || string.IsNullOrEmpty(transMessageId))
                return false;

            //请求的数据
            RequestMessage<OrderInfoModel> getReq = new RequestMessage<OrderInfoModel>(orderInfo, new RequestHead(TransTypeEnum.Access_tokenQuery, transMessageId));

            string accessToken = AccessTokenManage.GetAccessToken();
            getReq.head.transType = 200;
            string param = getReq.GetSerializeString();
            ResponseMessage<OrderCreateResponse> tokenResult = HttpHelper.PostJson<ResponseMessage<OrderCreateResponse>>(ShunFengUrl.GetRequestUrl(RequestUrlEnum.OrderCreate, accessToken, null, "v1.0", "rest"), param);

            if (tokenResult == null || tokenResult.head == null)
                return false;


            if (tokenResult.head.code == "EX_CODE_OPENAPI_0200")
            {
                return true;
            }

            //AccessToken过期或者不存在 刷新AccessToken后重试
            if (tokenResult.head.code == "EX_CODE_OPENAPI_0103" || tokenResult.head.code == "EX_CODE_OPENAPI_0105")
            {
                accessToken = AccessTokenManage.RefreshToken();
                //更新AccessToken再尝试
                tokenResult = HttpHelper.PostJson<ResponseMessage<OrderCreateResponse>>(ShunFengUrl.GetRequestUrl(RequestUrlEnum.Access_tokenQuery, accessToken), param);

                if (tokenResult == null || tokenResult.head == null)
                    return false;
                if (tokenResult.head.code == "EX_CODE_OPENAPI_0200")
                {
                    return true;
                }
            }
            return false;

        }
        #endregion

        #region 查询快递单 + OrderQueryResponse OrderQuery(string orderId, string transMessageId)
        /// <summary>
        /// 查询快递单
        /// </summary>
        /// <param name="orderId">快递单编号</param>
        /// <param name="transMessageId">新的交易流水号（保证唯一即可）</param>
        public OrderQueryResponse OrderQuery(string orderId, string transMessageId)
        {
            //请求的数据
            RequestMessage<OrderQueryModel> getReq = new RequestMessage<OrderQueryModel>(new OrderQueryModel(orderId), new RequestHead(TransTypeEnum.OrderQuery, transMessageId));

            string accessToken = AccessTokenManage.GetAccessToken();
            string param = getReq.GetSerializeString();
            //发送请求返回结果
            ResponseMessage<OrderQueryResponse> tokenResult = HttpHelper.PostJson<ResponseMessage<OrderQueryResponse>>(ShunFengUrl.GetRequestUrl(RequestUrlEnum.OrderQuery, accessToken,"","v1.0","rest"), param);

            if (tokenResult == null || tokenResult.head == null)
                return null;


            if (tokenResult.head.code == "EX_CODE_OPENAPI_0200")
            {
                return tokenResult.body;
            }

            //AccessToken过期或者不存在  刷新AccessToken后重试
            if (tokenResult.head.code == "EX_CODE_OPENAPI_0103" || tokenResult.head.code == "EX_CODE_OPENAPI_0105")
            {
                accessToken = AccessTokenManage.RefreshToken();
                //更新AccessToken再尝试
                tokenResult = HttpHelper.PostJson<ResponseMessage<OrderQueryResponse>>(ShunFengUrl.GetRequestUrl(RequestUrlEnum.OrderQuery, accessToken), param);

                if (tokenResult == null || tokenResult.head == null)
                    return null;
                if (tokenResult.head.code == "EX_CODE_OPENAPI_0200")
                {
                    return tokenResult.body;
                }
            }
            return null;
        }
        #endregion

    }
}
